# -*- coding: utf-8 -*-
import scrapy
from chanel.items import ChanelItem


class ChanelShopsSpider(scrapy.Spider):
    name = 'chanel_shops'
    allowed_domains = ['www.chanel.cn']
    base_url = 'https://www.chanel.cn{}'
    start_urls = ['https://www.chanel.cn/zh_CN/fragrance-beauty/fragrance.html']

    def parse(self, response):
        vo = response.css('div.fnb_thumblist_cols ul li')
        for v in vo:
            url = v.css('li a::attr(href)').get()
            url = self.base_url.format(url)
            yield response.follow(url=url, callback=self.parse_model)

    def parse_model(self, response):
        item = ChanelItem()
        vo = response.css('.breadcrumb .fnb_breadcrumb')
        item['head'] = vo.css('li:nth-child(1) a::text').get()
        item['type'] = vo.css('li:nth-child(2) a::text').get()
        item['series'] = vo.css('li:nth-child(3) span::text').get()
        vc = response.css('.row .fnb_inline')
        for v in vc:
            pic = v.css('.fnb_product-img a img::attr(src)').get()
            item['pic'] = self.base_url.format(pic)
            item['title'] = v.css('.fnb_product-img a img::attr(alt)').get().replace(' ', '').replace('\t', '')
            price = v.css('.fnb_inline-block .fnb_product-price::text').get()
            item['price'] = price.replace(' ', '').replace('\n', '').replace('\t', '')
            print(item['title'], item['price'])
            yield item
